/*
 * MIT License
 * 
 * Copyright (c) 2017 wen.gu <454727014@qq.com>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: vos_thread.h
 *
 * Purpose: virtual os layer thread interface define 
 *
 * Developer:
 *   wen.gu , 2014-06-07
 *
 * TODO: 
 *
 ***************************************************************************/

#ifndef __VOS_THREAD_H__
#define __VOS_THREAD_H__

#include "general_type.h"
#include "general_error.h"

#include <stdio.h>

#ifdef __cplusplus
extern "C"{
#endif
/***************************************************************************
 *
 * macro declaration
 *
 ***************************************************************************/
//the max priority mean highest priority. 
#define VTHD_PRIORITY_MAX 100
#define VTHD_PRIORITY_MIN 1

/** default priority,user can use this */
#define VTHD_DEFAULT_PRIORITY 0

//thread name maxim length
#define THD_NAME_MAX_LEN    31

/** invalid thread id define */
#define VTHD_INVALID_TID (vos_thread_id)(-1)
/***************************************************************************
 *
 * data struct declaration
 *
 ***************************************************************************/


//thread attribute define
typedef struct vos_thread_attr_s 
{
    GS32 priority; /** thread priority(range: lowest 0 - 100 highest) */ 

    /**
     * GTRUE:  need call vosThreadJoin to wait thread exit and release thread 
     *         resource
     *         
     * GFALSE: need call nothing,thread resource will be auto release.     
     */
    GBOL joinable;

    /**
     * thread name, if NULL,then thread name will be set as "unknown"
     */
    const GS08* name;
}vos_thread_attr_t;
/***************************************************************************
 *
 * thread API declaration
 *
 ***************************************************************************/

/**
*@brief  thread loop function type
*
*@param  
*
*@return if success exit return G_OK , else return error code.
*@see 
*/
typedef GS32 (*vosThreadFunc_t)(void *); 

//thread id type define
typedef void* vos_thread_id;

/************************************************************************
 * thread API
 ************************************************************************/
/**
*@brief  get current thread ID
*
*@param  None
*
*@return return thread ID
*@see 
*/
G_API vos_thread_id vosThreadGetId(void);
/**
*@brief  create a thread instance
*
*@param  threadId  [io] thread id output
*@param  loopFunc  [in] thread main loop function.
*@param  loopParam [in] thread loop function's parameter.
*@param  attr      [in] thread attribute. 
*
*@return if success return G_OK, else return G_FAIL.
*@see 
*@note   thread default state(the param attr is NULL) is joinable(mean that, usr 
*        need call function vosThreadjoin to release thread resource)
*        
*/
G_API GS32 vosThreadCreate(vos_thread_id *threadId, vosThreadFunc_t loopFunc, void* loopParam, vos_thread_attr_t* attr);

/**
*@brief  wait for thread to release resource
*
*@param  threadId [in] the id of thread.
*
*@return if sucess return G_OK(0),else return error code
*@see
*@note  if thread not in joinable state,then will be return 'G_ErrorInvalidOperation'
*/
G_API GS32 vosThreadJoin(vos_thread_id threadId);
#ifdef __cplusplus
}
#endif

#endif //end of __VOS_THREAD_H__
